package work.parabola.keyboardswitch.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import work.parabola.keyboardswitch.common.ResponseCode;
import work.parabola.keyboardswitch.common.ServerResponse;
import work.parabola.keyboardswitch.dao.AuthorDoMapper;
import work.parabola.keyboardswitch.dao.ManufacturerDoMapper;
import work.parabola.keyboardswitch.dao.SwitchDoMapper;
import work.parabola.keyboardswitch.dao.SwitchTypeDoMapper;
import work.parabola.keyboardswitch.pojo.query.keyswitch.AddSwitchQuery;
import work.parabola.keyboardswitch.pojo.query.keyswitch.EditSwitchQuery;
import work.parabola.keyboardswitch.pojo.query.keyswitch.ListQuery;
import work.parabola.keyboardswitch.pojo.repository.AuthorDo;
import work.parabola.keyboardswitch.pojo.repository.ManufacturerDo;
import work.parabola.keyboardswitch.pojo.repository.SwitchDo;
import work.parabola.keyboardswitch.pojo.repository.SwitchTypeDo;
import work.parabola.keyboardswitch.pojo.vo.keyswitch.SwitchListVo;
import work.parabola.keyboardswitch.pojo.vo.keyswitch.SwitchVo;
import work.parabola.keyboardswitch.service.ISwitchService;

import java.util.ArrayList;
import java.util.List;

@Service
public class SwitchServiceImpl implements ISwitchService {

    @Autowired
    private SwitchDoMapper switchDoMapper;

    @Autowired
    private ManufacturerDoMapper manufacturerDoMapper;

    @Autowired
    private AuthorDoMapper authorDoMapper;

    @Autowired
    private SwitchTypeDoMapper switchTypeDoMapper;

    @Override
    public ServerResponse getList(ListQuery listQuery) {
        if (listQuery == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(),
                    ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        ServerResponse response = listQuery.validate();
        if (!response.isSuccess()) {
            return response;
        }
        PageHelper.startPage(listQuery.getPageNum(), listQuery.getPageSize());
        List<SwitchDo> switchDoList = this.switchDoMapper.selectByParam(
                listQuery.getKeyword(), listQuery.getOnlyShowNoColor(),
                listQuery.getOnlyShowNoManufacturer()
        );
        List<SwitchListVo> switchListVoList = new ArrayList<>();
        for (SwitchDo switchDo : switchDoList) {
            switchListVoList.add(this.assembleSwitchListVo(switchDo));
        }
        PageInfo pageResult = new PageInfo(switchDoList);
        pageResult.setList(switchListVoList);
        return ServerResponse.createBySuccess(pageResult);
    }

    private SwitchListVo assembleSwitchListVo(SwitchDo switchDo) {
        SwitchListVo vo = new SwitchListVo();
        vo.setId(switchDo.getId());
        vo.setSimpleName(switchDo.getSimpleName());
        vo.setManufacturerId(switchDo.getManufacturerId());
        ManufacturerDo manufacturerDo = this.manufacturerDoMapper.selectByPrimaryKey(switchDo.getManufacturerId());
        if (manufacturerDo != null) {
            vo.setManufacturerName(manufacturerDo.getName());
        }
        vo.setAuthorId(switchDo.getAuthorId());
        AuthorDo authorDo = this.authorDoMapper.selectByPrimaryKey(switchDo.getAuthorId());
        if (authorDo != null) {
            vo.setAuthorName(authorDo.getName());
        }
        vo.setSwitchTypeId(switchDo.getSwitchTypeId());
        SwitchTypeDo switchTypeDo = this.switchTypeDoMapper.selectByPrimaryKey(switchDo.getSwitchTypeId());
        if (switchTypeDo != null) {
            vo.setSwitchTypeName(switchTypeDo.getName());
        }
        vo.setBackgroundColor(switchDo.getBackgroundColor());
        vo.setColor(switchDo.getColor());
        vo.setImg(switchDo.getImg());
        return vo;
    }

    @Override
    public ServerResponse getSwitchDetail(Integer id) {
        if (id == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        SwitchDo switchDo = this.switchDoMapper.selectByPrimaryKey(id);
        if (switchDo == null) {
            return ServerResponse.createByErrorMessage("轴体不存在");
        }
        SwitchVo switchVo = new SwitchVo();
        switchVo.setId(switchDo.getId());
        switchVo.setName(switchDo.getName());
        switchVo.setSimpleName(switchDo.getSimpleName());
        switchVo.setManufacturerId(switchDo.getManufacturerId());

        ManufacturerDo manufacturerDo = this.manufacturerDoMapper.selectByPrimaryKey(switchDo.getManufacturerId());
        if (manufacturerDo != null) {
            switchVo.setManufacturerName(manufacturerDo.getName());
        }
        switchVo.setAuthorId(switchDo.getAuthorId());

        AuthorDo authorDo = this.authorDoMapper.selectByPrimaryKey(switchDo.getAuthorId());
        if (authorDo != null) {
            switchVo.setAuthorName(authorDo.getName());
        }
        switchVo.setSwitchTypeId(switchDo.getSwitchTypeId());

        SwitchTypeDo switchTypeDo = this.switchTypeDoMapper.selectByPrimaryKey(switchDo.getSwitchTypeId());
        if (switchTypeDo != null) {
            switchVo.setSwitchTypeName(switchTypeDo.getName());
        }
        switchVo.setBackgroundColor(switchDo.getBackgroundColor());
        switchVo.setColor(switchDo.getColor());
        switchVo.setImg(switchDo.getImg());

        return ServerResponse.createBySuccess(switchVo);
    }

    @Override
    public ServerResponse editSwitch(EditSwitchQuery editSwitchQuery) {
        if (editSwitchQuery == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(),
                    ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        ServerResponse response = editSwitchQuery.validate();
        if (!response.isSuccess()) {
            return response;
        }
        SwitchDo switchDo = this.switchDoMapper.selectByPrimaryKey(editSwitchQuery.getId());
        if (switchDo == null) {
            return ServerResponse.createByErrorMessage("轴体不存在");
        }
        ManufacturerDo manufacturerDo = null;
        if (editSwitchQuery.getManufacturerId() != null) {
            manufacturerDo = this.manufacturerDoMapper.selectByPrimaryKey(editSwitchQuery.getManufacturerId());
            if (manufacturerDo == null) {
                return ServerResponse.createByErrorMessage("轴体代工厂错误");
            }
        }
        AuthorDo authorDo = null;
        if (editSwitchQuery.getAuthorId() != null) {
            authorDo = this.authorDoMapper.selectByPrimaryKey(editSwitchQuery.getAuthorId());
            if (authorDo == null) {
                return ServerResponse.createByErrorMessage("轴体作者不存在");
            }
        }
        SwitchTypeDo switchTypeDo = null;
        if (editSwitchQuery.getSwitchTypeId() != null) {
            switchTypeDo = this.switchTypeDoMapper.selectByPrimaryKey(editSwitchQuery.getSwitchTypeId());
            if (switchTypeDo == null) {
                return ServerResponse.createByErrorMessage("轴体类型不存在");
            }
        }
        SwitchDo updateRecord = new SwitchDo();
        updateRecord.setId(editSwitchQuery.getId());
        updateRecord.setName(editSwitchQuery.getName());
        updateRecord.setSimpleName(editSwitchQuery.getSimpleName());
        updateRecord.setManufacturerId(editSwitchQuery.getManufacturerId());
        updateRecord.setAuthorId(editSwitchQuery.getAuthorId());
        updateRecord.setSwitchTypeId(editSwitchQuery.getSwitchTypeId());
        updateRecord.setBackgroundColor(editSwitchQuery.getBackgroundColor());
        updateRecord.setColor(editSwitchQuery.getColor());
        int rowCount = switchDoMapper.updateByPrimaryKey(updateRecord);
        if (rowCount == 0) {
            return ServerResponse.createByErrorMessage("修改轴体失败");
        }
        return ServerResponse.createBySuccessMessage("修改成功");
    }

    @Override
    public ServerResponse addSwitch(AddSwitchQuery addSwitchQuery) {
        if (addSwitchQuery == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(),
                    ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        ServerResponse response = addSwitchQuery.validate();
        if (!response.isSuccess()) {
            return response;
        }
        int rowCount = this.switchDoMapper.selectCountBySimpleName(addSwitchQuery.getSimpleName());
        if (rowCount > 0) {
            return ServerResponse.createByErrorMessage("轴体简单名称重复");
        }
        ManufacturerDo manufacturerDo = null;
        if (addSwitchQuery.getManufacturerId() != null) {
            manufacturerDo = this.manufacturerDoMapper.selectByPrimaryKey(addSwitchQuery.getManufacturerId());
            if (manufacturerDo == null) {
                return ServerResponse.createByErrorMessage("轴体代工厂错误");
            }
        }
        AuthorDo authorDo = null;
        if (addSwitchQuery.getAuthorId() != null) {
            authorDo = this.authorDoMapper.selectByPrimaryKey(addSwitchQuery.getAuthorId());
            if (authorDo == null) {
                return ServerResponse.createByErrorMessage("轴体作者不存在");
            }
        }
        SwitchTypeDo switchTypeDo = null;
        if (addSwitchQuery.getSwitchTypeId() != null) {
            switchTypeDo = this.switchTypeDoMapper.selectByPrimaryKey(addSwitchQuery.getSwitchTypeId());
            if (switchTypeDo == null) {
                return ServerResponse.createByErrorMessage("轴体类型不存在");
            }
        }
        SwitchDo switchDo = new SwitchDo();
        switchDo.setName(addSwitchQuery.getName());
        switchDo.setSimpleName(addSwitchQuery.getSimpleName());
        switchDo.setManufacturerId(addSwitchQuery.getManufacturerId());
        switchDo.setAuthorId(addSwitchQuery.getAuthorId());
        switchDo.setSwitchTypeId(addSwitchQuery.getSwitchTypeId());
        switchDo.setBackgroundColor(addSwitchQuery.getBackgroundColor());
        switchDo.setColor(addSwitchQuery.getColor());
        rowCount = this.switchDoMapper.insertSelective(switchDo);
        if (rowCount == 0) {
            return ServerResponse.createByErrorMessage("增加轴体失败");
        }
        return ServerResponse.createBySuccessMessage("增加轴体成功");

    }
}
